/**
*
@cbs
 本文件基于以下JS包(API)和css包(css文件省略)集合而成    
 jquery.js                    http://jquery.com/                      v1.9.1
 jquery.form.js               http://malsup.com/jquery/form/
 layer.js                     http://sentsin.com/jquery/layer/
 bootstrap.min.js             http://v2.bootcss.com/index.html
 bootstrap-datepicker.js      http://www.eyecon.ro/bootstrap-datepicker/
 bootstrap-datetimepicker.js  http://www.eyecon.ro/bootstrap-datepicker/
 jquery.ketchup.all.js        http://demos.usejquery.com/ketchup-plugin/
 jquery.fileupload系列包       https://github.com/blueimp/jQuery-File-Upload
*
**/
var btd = {
    local: {},
    form: {},
    layer: {},
    datepicker: {},
    datetimepicker: {},
    subSomething: {},
    uploadfile: {},
    func: {},
    templ: {},
    loading: {}
};
btd.form = {
    actionTo: function () {
        $("form").each(function () {
            if ($(this).data("btd.form.actionTo"))
                return true;
            $(this).data("btd.form.actionTo", "btd.form.actionTo");
            if ($(this).attr("actionTo")) {
                var dataType = $(this).attr("dataType") ? $(this).attr("dataType") : "json";
                btd.form.ajax($(this), dataType);
            }/*else if ($(this).attr("editor")) btd.editor.magic(this,$(this).attr("editor"));*/
        });
    },
    showRequest: function (formData, $form, options) {
        if ($form.attr("beforeSubmit")) {
            var queryString = $.param(formData);
            btd.form.cal($form, "beforeSubmit", queryString);
        }
        return true;
    },
    showResponse: function (responseText, statusText, xhr, $form, cal) {
        if ($form.attr("dataType")) {
            if ($form.attr("dataType") == "script") {
                var obj = new Function("return " + responseText)();
            } else if ($form.attr("dataType") == "json") {
                var obj = $.parseJSON(responseText);
            } else {
                if ($form.attr("callback")) {
                    btd.form.cal($form, "callback", queryString);
                }
                return;
            }
        }
        else
            var obj = $.parseJSON(responseText);
        var $table = $($form.attr("actionTo"));
        if ($table.length < 1) {
            if (typeof cal == "undefined") {
                if ($form.attr("callback"))
                    btd.form.cal($form, "callback", responseText);
            } else if (cal == "false" || cal == false)
                return;
            else if ($form.attr("callback")) btd.form.cal($form, "callback", responseText);
            return;
        } 
        var $th = $table.find("thead th");
        var order = [];
        for (var j = 0; j < $th.length; j++) {
            order.push($($th[j]).attr("act"));
        }
        var tr = "", totalRow = 0, listRow = 0, nowPage = 0;
        for (var i = 0; i < obj.length; i++) {
            for (var k = 0; k < order.length; k++) {
                if (i % 2 == 0) {
                    if (k == 0) {
                        if (obj[i][order[k]]) {
                            tr += "<tr class='gradeA odd'>"
                            tr += "<td>" + obj[i][order[k]] + "</td>";
                            if (k == order.length - 1)
                                tr += "</tr>"
                        } else
                            break;
                    } else if (k == order.length - 1) {
                        tr += "<td>" + obj[i][order[k]] + "</td></tr>";
                    } else {
                        tr += "<td>" + obj[i][order[k]] + "</td>";
                    }
                } else {
                    if (k == 0) {
                        if (obj[i][order[k]]) {
                            tr += "<tr class='gradeA event'>"
                            tr += "<td>" + obj[i][order[k]] + "</td>";
                            if (k == order.length - 1)
                                tr += "</tr>"
                        } else
                            break;
                    } else if (k == order.length - 1) {
                        tr += "<td>" + obj[i][order[k]] + "</td></tr>";
                    } else {
                        tr += "<td>" + obj[i][order[k]] + "</td>";
                    }
                }
            }
            if (obj[i]["totalRow"]) {
                totalRow = parseInt(obj[i]["totalRow"]);
            }
            if (obj[i]["listRow"]) {
                listRow = parseInt(obj[i]["listRow"]);
            }
            if (obj[i]["nowPage"]) {
                nowPage = parseInt(obj[i]["nowPage"]);
            }
        }
        $table.find("tbody").html(tr);
        tpage.page(totalRow, listRow, nowPage, $table.parent().find(".fg-toolbar"));
        btd.layer.enter($table.parent().find(".fg-toolbar"));
        if (typeof cal == "undefined") {
            if ($form.attr("callback"))
                btd.form.cal($form, "callback", responseText);
        } else if (cal == "false" || cal == false)
            return;
        else
            if ($form.attr("callback"))
                btd.form.cal($form, "callback", responseText);

    },
    ajax: function ($form, dataType) {
        var options = {
            beforeSubmit: btd.form.showRequest,
            success: btd.form.showResponse,
            dataType: dataType,
            timeout: 3000,
            error: function (e) {
                alert("ajax错误:" + e);
            }
        };
        $form.ajaxForm(options);
    },
    cal: function (col, name, queryString) {
        var $col = $(col);
        if ($col.attr(name).indexOf("(") > -1)
            var fun = $col.attr(name).split("(")[0];
        else
            var fun = $col.attr(name);
        var func = eval(fun);
        return func(queryString);
    },
    getformval: function (form, json) {
        var $form = $(form);
        $form.each(function () {
            if ($(this).children())
                btd.form.getformval($(this).children(), json);
            if ($(this).attr("name")) {
                if ($(this).val())
                    json[$(this).attr("name")] = "", json[$(this).attr("name")] = $(this).val();
                else if ($(this).attr("value"))
                    json[$(this).attr("name")] = "", json[$(this).attr("name")] = $(this).attr("value");
            }
        });
        return json;
    },
    a_select: function (t, form) {
        var $t = $(t), $form = $(form);
        $t.each(function () {
            if ($(this).children())
                btd.form.a_select($(this).children(), form);
            if ($(this).data("btd.form.a_select")) {
                return;
            }
            if ((this.tagName == 'a' || this.tagName == 'A') && $(this).attr('href')) {
                $(this).on("click", function (e) {
                    e.preventDefault();
                    if ($form.attr("form")) {
                        if ($form.attr("form") != "false") {
                            var json = btd.form.getformval($($form.attr("form")), {});
                            var queryString = $.param(json);
                        }
                    }
                    var queryString = queryString ? queryString + "&nowpage=" + $(this).attr('href') : $(this).attr('href');
                    //"&listrow=" + $form.find("select>option:selected").attr("value")
                    $.ajax({
                        url: $form.attr("action").toString(),
                        beforeSend: function () {
                            if ($form.attr("beforeSubmit"))
                                btd.form.cal($form, "beforeSubmit", queryString);
                        },
                        data: queryString,
                        dataType: $form.attr("dataType") || "json",
                        success: function (msg) {
                            btd.form.showResponse(msg, "succes", "", $($form.attr("form")), false);
                            if ($form.attr("callback"))
                                btd.form.cal($form, "callback", msg);
                        }
                    });
                });
            } else if (this.tagName == 'select' || this.tagName == 'SELECT') {
                $(this).on("change", function (e) {
                    var t_json = btd.form.attrget($($form.attr("form")), "page", []);
                    for (var i = 0; i < t_json.length; i++) {
                        if ($(t_json[i].attr("page"))[0] == $form[0]) {
                            t_json[i].attr("value", $(this).find("option:selected").attr("value"));
                        }
                    }
                });
            }
            $(this).data("btd.form.a_select", "btd.form.a_select");
        });
    },
    attrget: function (t, attr, json) {
        var $t = $(t);
        $t.each(function () {
            if ($(this).children())
                btd.form.attrget($(this).children(), attr, json);
            if ($(this).attr(attr)) {
                json.push($(this));
            }
        })
        return json;
    }
}
btd.layer = {
    alert: function (o) {
        var o = $.extend({ alertMsg: "请输入信息!", alertType: 0, alertTit: false, t: { alertYes: function () { } } }, o);
        var loadi = layer.alert(o.alertMsg, o.alertType, o.alertTit, function () { layer.close(loadi); o.t.alertYes(); });
    },
    confirm: function (o) {
        var o = $.extend({ conMsg: "请输入信息!", conTit: "信息", t: { conYes: function () { }, conNo: function () { } } }, o);
        var loadi = layer.confirm(o.conMsg, function () { layer.close(loadi); o.t.conYes() }, o.conTit, function () { layer.close(loadi); o.t.conNo() });
    },
    msg: function (o) {
        var o = $.extend({ msgText: "请输入信息!", msgTime: 2, parme: false, t: { callback: function () { } } }, o);
        layer.msg(o.msgText, o.msgTime, o.parme);
        setTimeout(function () { o.t.callback() }, o.msgTime * 1000 + 200);
    },
    tips: function (o) {
        var o = $.extend({ html: "请输入信息!", follow: "", parme: { time: 1, style: ['background-color:#0FA6D8; color:#fff', '#0FA6D8']} }, o);
        layer.tips(o.html, o.follow, o.parme)
    },
    layer: function (o) {
        if (o.closes)
            o.close = function (index) { var json = btd.layer.getChildrenJson(layer.getChildFrame('body'), []); o.closes(json); layer.close(index); }
        var o = $.extend({
            type: 2,
            title: false,
            iframe: { src: 'http://www.baidu.com' },
            area: ['1000px', '500px'],
            success: function () { },
            close: function (index) { layer.close(index); }
        }, o);
        $.layer(o);
    },
    enter: function (t, test) {
        var $t = $(t);
        $t.each(function () {
            if ($(this).children())
                btd.layer.enter($(this).children());
            if ($(this).data("btd.layer.enter")) {
                return true;
            }
            if ($(this).attr("layer")) {
                $(this).on("click", function () {
                    btd.layer.magic(this, $(this).attr("layer").toString());
                });
            } else if ($(this).attr("layerchildren")) {
                btd.layer.magic(this, $(this).attr("layerchildren").toString());
            } else if ($(this).attr("tooltip")) {
                btd.layer.magic(this, $(this).attr("tooltip").toString());
            }
            if ($(this).attr("form")) {
                btd.form.a_select($(this), this);
            }
            if ($(this).attr("page")) {
                $(this).attr("value", $($(this).attr("page")).find("option:selected").attr("value"));
            }
            if ($(this).attr("data-toggle")) {
                if ($(this).attr("data-toggle") == "hover")
                    $(this).hover(function () {
                        $(this).addClass("open");
                    }, function () {
                        $(this).removeClass("open");
                    });
            }
            if ($(this).attr("data-date-format")) {
                $(this).datepicker();
                if ($(this).attr("date-now"))
                    if ($(this).attr("date-now") == "false") return;

                $(this).attr("value", btd.datepicker.getNowDate(btd.local.getlimit(this), this));

            }
            if ($(this).attr("data-format")) {
                $(this).parent().datetimepicker({ language: 'zh-CN' });
                if ($(this).attr("date-now"))
                    if ($(this).attr("date-now") == "false") return;
                $(this).attr("value", btd.datetimepicker.getNowDate(btd.local.getlimit(this), this));

            }
            if ($(this).attr("data-validate-type")) {
                if (parseInt($(this).attr("data-validate-type")) == 1) {
                    if ($(this).data("ketchup")) {
                        return;
                    }
                    $(this).data("ketchup", "ketchup");
                    $(this).ketchup();
                } else {
                    if (!$('body').data("createErrorContainer")) {
                        $('body').data("createErrorContainer", "createErrorContainer");
                        $.ketchup.createErrorContainer(function (form, el) {
                            return $('<ul/>', {
                                'class': 'ketchup-custom'
                            }).insertAfter(el);
                        }).addErrorMessages(function (form, el, container, messages) {
                            container.html('');

                            for (i = 0; i < messages.length; i++) {
                                $('<li/>', {
                                    text: messages[i]
                                }).appendTo(container);
                            }
                        }).showErrorContainer(function (form, el, container) {
                            container.slideDown('fast');
                        }).hideErrorContainer(function (form, el, container) {
                            container.slideUp('fast');
                        });
                    }
                    $(this).ketchup({
                        validateEvents: 'blur keyup'
                    });
                }
            }
            if ($(this).attr("file-type")) btd.uploadfile.magic(this, parseInt($(this).attr("file-type")));
            if ($(this).attr("editor")) btd.uploadfile.magic(this, parseInt($(this).attr("editor")));
            $(this).data("btd.layer.enter", "btd.layer.enter");
        });
    },
    magic: function (t, lay) {
        var $t = $(t);
        switch (lay) {
            case "alert":
                t.alertYes = function () { try { eval($t.attr("alertYes")) } catch (err) { alert(err) } };
                btd.layer.alert({ alertMsg: $t.attr("alertMsg"), alertType: $t.attr("alertType"), alertTit: $t.attr("alertTit"), t: t });
                break;
            case "confirm":
                t.conYes = function () { try { eval($t.attr("conYes")) } catch (err) { alert(err); } }, t.conNo = function () { try { eval($t.attr("conNo")) } catch (err) { alert(err) } };
                btd.layer.confirm({ conMsg: $t.attr("conMsg"), conTit: $t.attr("conTit"), t: t });
                break;
            case "msg":
                t.callback = function () { try { eval($t.attr("callback")); } catch (err) { alert(err); } };
                btd.layer.msg({ msgText: $t.attr("msgText"), msgTime: parseInt($t.attr("msgTime") || 2), t: t });
                break;
            case "tips":
                var parme = { time: 2, style: ['background-color:#0FA6D8; color:#fff', '#0FA6D8'] };
                if ($t.attr("parme")) {
                    var p1 = new Function("return " + $t.attr("parme"))();
                    parme = $.extend(parme, p1);
                }
                btd.layer.tips({ html: $t.attr("html"), follow: t, parme: parme });
                break;
            case "load":
                if ($t.attr("loadIcon")) {
                    var load = isNaN($t.attr("parme")) ? layer.load($t.attr("parme"), parseInt($t.attr("loadIcon"))) : layer.load(parseInt($t.attr("parme")), parseInt($t.attr("loadIcon")));
                } else {
                    var load = isNaN($t.attr("parme")) ? layer.load($t.attr("parme")) : layer.load(parseInt($t.attr("parme")));
                }
                if ($t.attr("times")) {
                    setTimeout(function () { layer.close(load); }, parseInt($t.attr("times")) * 1000);
                }
                else if ((!$t.attr("times") && ($t.attr("parme") == "0" || $t.attr("parme") == 0)) && $t.attr("close")) {
                    btd.form.cal($t, "close", load); break;
                }
                if ($t.attr("callback")) {
                    t.callback = function () { try { eval($t.attr("callback")) } catch (err) { alert(err) } };
                    if (isNaN($t.attr("parme"))) {
                        if ($t.attr("times")) {
                            setTimeout(function () { t.callback() }, parseInt($t.attr("times")) * 1000 + 200);
                        }
                    } else {
                        setTimeout(function () { t.callback() }, parseInt($t.attr("parme")) * 1000 + 200);
                    }
                }
                break;

            case "layer":
                var options = { type: 2, title: false, iframe: { src: 'http://www.baidu.com' }, area: ['1000px', '500px'] };
                options.closes = function () { };
                if ($t.attr("options"))
                    options = new Function("return " + $t.attr("options"))();
                if ($t.attr("success")) {
                    var suc = new Function("return " + $t.attr("success"))();
                    options.success = function () {
                        new Function("direction", "time", "layer.shift(direction,time);")(suc.direction, parseInt(suc.time));
                    }
                }
                if ($t.attr("close")) {
                    if ($t.attr("close").indexOf("(") > -1)
                        var fun = $t.attr("close").split("(")[0];
                    else
                        var fun = $t.attr("close");
                    var clo = eval(fun);
                    options.closes = clo;
                }
                if ($t.attr("setaccept")) {
                    if (window.layer)
                        window.layer.accept = $t.attr("setaccept").toString();
                    else {
                        window.layer = {};
                        window.layer.accept = $t.attr("setaccept").toString();
                    }

                }
                btd.layer.layer(options);
                break;
            case "children":
                index = parent.layer.getFrameIndex(window.name);
                break;
            case "close":
                $t.on("click", function () {
                    var json = btd.layer.getChildrenJson($('body'), []);
                    if ($t.attr("close")) {
                        if ($t.attr("close").indexOf("(") > -1)
                            var fun = $t.attr("close").split("(")[0];
                        else
                            var fun = $t.attr("close");
                        var clo = parent.eval(fun);
                        clo(json);
                    }
                    parent.layer.close(index);
                });
                break;
            case "iframeAuto":
                parent.layer.iframeAuto(index);
                break;
            case "popover":
                $t.popover();
                break;
            case "tooltip":
                $t.tooltip();
                break;
            case "clickget":
                $t.on("click", function () {
                    if ($(this).attr("value"))
                        var value = $(this).attr("value");
                    else if (typeof $(this).val() != "undefined")
                        var value = $(this).val();
                    else
                        var value = $(this).html();
                    var json = { name: $t.attr("name"), value: value };

                    btd.layer.setAccept(parent.document.getElementsByTagName('body'), json)
                    parent.layer.close(index);
                });
                break;
            case "click2get":
                $t.on("dblclick", function () {
                    if ($(this).attr("value"))
                        var value = $(this).attr("value");
                    else if (typeof $(this).val() != "undefined")
                        var value = $(this).val();
                    else
                        var value = $(this).html();
                    var json = { name: $t.attr("name"), value: value };

                    btd.layer.setAccept(parent.document.getElementsByTagName('body'), json)
                    parent.layer.close(index);
                });
                $t.css("cursor", "pointer")
                break;
            case "clickgetid":
                $t.on("click", function () {
                    if ($(this).attr("value"))
                        var value = $(this).attr("value");
                    else if (typeof $(this).val() != "undefined")
                        var value = $(this).val();
                    else
                        var value = $(this).html();
                    var json = { name: $t.attr("name"), value: value };

                    btd.layer.setAcceptId(parent.document.getElementsByTagName('body'), json)
                    parent.layer.close(index);
                });
                $t.css("cursor", "pointer")
                break;
            case "click2getid":
                $t.on("dblclick", function () {
                    if ($(this).attr("value"))
                        var value = $(this).attr("value");
                    else if (typeof $(this).val() != "undefined")
                        var value = $(this).val();
                    else
                        var value = $(this).html();
                    var json = { name: $t.attr("name"), value: value };

                    btd.layer.setAcceptId(parent.document.getElementsByTagName('body'), json)
                    parent.layer.close(index);
                });
                $t.css("cursor", "pointer")
                break;

        }
    },
    getChildrenJson: function (t, json) {
        var $t = $(t);
        $t.each(function () {
            if ($(this).children())
                btd.layer.getChildrenJson($(this).children(), json);
            if ($(this).attr("layerchildren")) {
                if ($(this).attr("layerchildren") == "layerget") {
                    if ($(this).attr("value"))
                        var value = $(this).attr("value");
                    else if (typeof $(this).val() != "undefined")
                        var value = $(this).val();
                    else
                        var value = $(this).html();
                    json.push({ name: $(this).attr("name"), value: value });
                }
            }
        });
        return json;
    },
    setAccept: function (t, json) {
        var $t = $(t);
        $t.each(function () {
            if ($(this).attr("accept")) {
                if ($(this).attr("accept") == parent.layer.accept) {
                    if ($(this).val())
                        $(this).val(json.value)
                    else
                        $(this).attr("value", json.value)
                    $(this).attr("name", json.name)
                    return false;
                } else if ($(this).children())
                    btd.layer.setAccept($(this).children(), json);
            } else
                btd.layer.setAccept($(this).children(), json);
        });
    },
    setAcceptId: function (t, json) {
        var $t = $(t);
        $t.each(function () {
            if ($(this).attr("accept")) {
                if ($(this).attr("accept") == parent.layer.accept) {
                    $(this).val(json.name)
                    $(this).attr("value", json.value)
                    $(this).attr("name", json.name)
                    return false;
                } else if ($(this).children())
                    btd.layer.setAcceptId($(this).children(), json);
            } else
                btd.layer.setAcceptId($(this).children(), json);
        });
    }
}
btd.uploadfile = {
    magic: function (t, lay) {
        sdF.useCss(["css/blueimp-gallery.min.css"]);
        switch (lay) {
            case 1:
                sdF.getJs(["jquery.ui.widget.js", "jquery.iframe-transport.js", "jquery.fileupload.js"], function () {
                    upload.file1(t);
                });
                break;
            case 2:
                sdF.getJs(["jquery.ui.widget.js", "load-image.min.js", "canvas-to-blob.min.js", "jquery.iframe-transport.js", "jquery.fileupload.js", "jquery.fileupload-audio.js", "jquery.fileupload-process.js", "jquery.fileupload-image.js", "jquery.fileupload-video.js", "jquery.fileupload-validate.js"], function () {
                    upload.file2(t);
                });
                break;
            case 3:
                sdF.getJs(["canvas-to-blob.min.js", "jquery.ui.widget.js", "jquery.fileupload.js", "tmpl.min.js", "load-image.min.js", "jquery.blueimp-gallery.min.js", "jquery.iframe-transport.js", "jquery.fileupload-audio.js", "jquery.fileupload-ui.js", "jquery.fileupload-process.js", "jquery.fileupload-image.js", "jquery.fileupload-video.js", "jquery.fileupload-validate.js"],function(){
                    upload.file3(t);
                });//, "main.js"
                break;
            /*case 4:
                sdF.getJs(["angular.min.js","jquery.ui.widget.js","load-image.min.js","canvas-to-blob.min.js","jquery.blueimp-gallery.min.js","jquery.iframe-transport.js","jquery.fileupload.js","jquery.fileupload-process.js","jquery.fileupload-image.js","jquery.fileupload-audio.js","jquery.fileupload-video.js", "jquery.fileupload-validate.js","jquery.fileupload-angular.js","app.js"]); 
                break;*/
        }
    }
}
btd.local = {
    getlimit: function (t) {
        var $t = $(t), limit = "-";
        if ($t.attr("data-date-format")) {
            var d = $t.attr("data-date-format");
            limit = d.indexOf('-') > -1 ? '-' : (d.indexOf('/') > -1 ? '/' : (d.indexOf('.') > -1 ? '.' : '-'));
        }
        return limit;
    },
    checkTime: function (i) {
        if (i < 10)
            i = "0" + i;
        return i;
    }
}
btd.datepicker = {
    getNowDate: function (limit, t) {
        var nowTemp = new Date(), $t = $(t);
        var df = $t.attr('data-date-format'), re = nowTemp.getFullYear();
        if (df) {
            re = df.indexOf('M') > -1 || df.indexOf('m') > -1 ? df.indexOf('D') > -1 || df.indexOf('d') > -1 ? re + limit + btd.local.checkTime(nowTemp.getMonth() + 1) + limit + btd.local.checkTime(nowTemp.getDate()) : re + limit + btd.local.checkTime(nowTemp.getMonth() + 1) : re;
            return re;
        }
        else return nowTemp.getFullYear() + limit + btd.local.checkTime(nowTemp.getMonth() + 1) + limit + btd.local.checkTime(nowTemp.getDate());
    }
}
btd.datetimepicker = {
    getNowDate: function (limit, t) {
        var nowTemp = new Date(), $t = $(t);
        var df = $t.attr('data-format'), re = nowTemp.getFullYear() + limit + btd.local.checkTime(nowTemp.getMonth() + 1) + limit + btd.local.checkTime(nowTemp.getDate());
        if (df) {
            re = df.indexOf('h') > -1 || df.indexOf('H') > -1 ? df.indexOf(':m') > -1 ? df.indexOf(':s') > -1 || df.indexOf(':S') > -1 ? re + " " + btd.local.checkTime(nowTemp.getHours()) + ":" + btd.local.checkTime(nowTemp.getMinutes()) + ":" + btd.local.checkTime(nowTemp.getSeconds()) : re + " " + btd.local.checkTime(nowTemp.getHours()) + ":" + btd.local.checkTime(nowTemp.getMinutes()) : re + " " + btd.local.checkTime(nowTemp.getHours()) : re;
            return re;
        }
        else return re;
    }
}
btd.loadSomething = {
    complete: function () {
        //sdF.$_start();
    },
    onreadystatechange: function (o) {
        var arr = [];
        if (o) {
            arr = btd.func.getProp(o);
        }
        
        switch (document.readyState) {
            case "loading":
                break;
            case "interactive": //ie没有
                var f = {};
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] == "interactive") {
                        f = $.extend({ beform: function () { }, callback: function () { } }, o[arr[i]]);
                        break;
                    };

                }
                if (f.beform) f.beform();

                if (f.callback) f.callback();
                break;
            case "complete":
                var f = {}
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] == "complete") {
                        f = $.extend({ beform: function () { }, callback: function () { } }, o[arr[i]]);
                        break;
                    };

                }
                if (f.beform) f.beform();
                btd.loadSomething.complete();
                if (f.callback) f.callback();
                break;
        }
    }
}
btd.func = {
    getProp: function (o) {
        var arr = [];
        for (var i in o) {
            arr.push(i);
        }
        return arr;
    },
    bodyhide: function () {
        var startTime = new Date().getTime(), bgdiv = false, loadingobj = false;
        btd.templ.bodyhide = window.self.setInterval(function () {
            if (!document.body) {
                return;
            }
            if (typeof (btd.loading) != "undefined" ? !btd.loading ? false : true : true) {
                if (!bgdiv) {
                    bgdiv = true;
                    var bodyWidth = document.documentElement.clientWidth;
                    var bodyHeight = document.documentElement.clientHeight;
                    var bgObj = document.createElement("div");
                    bgObj.setAttribute('id', 'bgDiv');
                    bgObj.style.position = "absolute";
                    bgObj.style.top = "0";
                    bgObj.style.background = "#F5F5F5";
                    bgObj.style.left = "0";
                    bgObj.style.width = bodyWidth + "px";
                    bgObj.style.height = bodyHeight + "px";
                    bgObj.style.zIndex = "10000";
                    document.body.appendChild(bgObj);
                }
                if ((new Date().getTime() - startTime >= 1200) && !loadingobj) {
                    oadingobj = true;
                    window.clearInterval(btd.templ.bodyhide);
                    var head = document.getElementsByTagName("head")[0];
                    var js = document.scripts || $('script'), jsPath = js[0].src;
                    var jsPath1 = jsPath.substring(0, jsPath.lastIndexOf("/"));
                    var cssPath = jsPath1.substring(0, jsPath1.lastIndexOf("/") + 1);
                    var bodyWidth = document.documentElement.clientWidth;
                    var bodyHeight = document.documentElement.clientHeight;
                    var loadingObj = document.createElement("div");
                    loadingObj.setAttribute('id', 'loadingDiv');
                    loadingObj.style.position = "absolute";
                    loadingObj.style.top = bodyHeight / 2 - 16 + "px";
                    loadingObj.style.left = bodyWidth / 2 - 16 + "px";
                    loadingObj.style.background = "url(" + cssPath + "img/xubox_loading2.gif) no-repeat";
                    loadingObj.style.width = "32px";
                    loadingObj.style.height = "32px";
                    loadingObj.style.zIndex = "10000";
                    document.body.appendChild(loadingObj);
                }
            }
        }, 20);

    },
    bodyshow: function () {
        var s = window.self.setInterval(function () {
            window.clearInterval(s);
            if (btd.templ.bodyhide) window.clearInterval(btd.templ.bodyhide);
            $("#loadingDiv").remove();
            $("#bgDiv").remove();
        }, 20);
    }
}

var bt = {
    layer: function () {
        btd.layer.enter($('body'));
    },
    form: function () {
        btd.form.actionTo();
    },
    start: function () {
        for (var i in bt) {
            if (i == "start")
                continue;
            else
                bt[i]();
        }
    }
}
var sdF = {
    searchFile: function (name) {
        var arr = [], paths = document.scripts || $('script');
        if (!$(window).data("getJs")) {
            for (var i = 0; i < paths.length; i++) {
                var jsPath = paths[i].src;
                jsPath = jsPath.substring(jsPath.lastIndexOf("/") + 1, jsPath.length);
                arr.push(jsPath);
            }
            $(window).data("getJs", arr);
        } else {
            arr = $(window).data("getJs");
        }
        for (var i = 0; i < name.length; i++) {
            for (var j = 0; j < arr.length; j++) {
                if (!name[i]) continue;
                if (arr[j] == name[i].replace(/(^\s*)|(\s*$)/g, "")) {
                    name.splice(i, 1)
                }
            }
        }
        return name;
    },
    useJs: function (name, callback) {
        var head = document.getElementsByTagName("head")[0];
        var js = document.scripts || $('script'), jsPath = js[0].src;
        jsPath = jsPath.substring(0, jsPath.lastIndexOf("/") + 1);
        for (var i = 0; i < name.length; i++) {
            var node = document.createElement('script');
            node.setAttribute('src', jsPath + name[i]);
            head.appendChild(node);
        }
        if (typeof callback == "function") callback();

    },
    getJs: function (name, index, jsPath, callback) {
        if (index) {
            if (!isNaN(index)) {
                $(window).data("getJs").push(name[index]);

                $.getScript(jsPath + name[index]).done(function (script, textStatus) {
                    if (index + 1 < name.length) { sdF.getJs(name, index + 1, jsPath, callback || function () { }); }
                    else {
                        callback(script, textStatus);
                    }
                }).fail(function (jqxhr, settings, exception) {
                    if (index + 1 < name.length) { sdF.getJs(name, index + 1, jsPath, callback || function () { }); }
                    else callback(jqxhr, settings, exception);
                });
            } else {
                var head = document.getElementsByTagName("head")[0];
                var js = document.scripts || $('script'), jsPath = js[0].src;
                jsPath = jsPath.substring(0, jsPath.lastIndexOf("/") + 1);
                var name = sdF.searchFile(name);

                if (name.length > 1) {
                    $(window).data("getJs").push(name[0]);
                    $.getScript(jsPath + name[0]).done(function () {
                        sdF.getJs(name, 1, jsPath, index);
                    }).fail(function () {
                        sdF.getJs(name, 1, jsPath, index);
                    });
                }
                else if (name.length == 1) {
                    $(window).data("getJs").push(name[0]);
                    $.getScript(jsPath + name[0]).done(function () {
                        index();
                    }).fail(function () {
                        index();
                    });
                }
                else index();
            }
        } else {
            var head = document.getElementsByTagName("head")[0];
            var js = document.scripts || $('script'), jsPath = js[0].src;
            jsPath = jsPath.substring(0, jsPath.lastIndexOf("/") + 1);
            var name = sdF.searchFile(name);
            if (name.length > 1) {
                $(window).data("getJs").push(name[0]);
                $.getScript(jsPath + name[0]).done(function () { sdF.getJs(name, 1, jsPath) }).fail(function () { sdF.getJs(name, 1, jsPath) });
            }
            else if (name.length == 1) {
                $(window).data("getJs").push(name[0]);
                $.getScript(jsPath + name[0]);
            }
        }
    },
    useCss: function (name) {
        var head = document.getElementsByTagName("head")[0];
        var js = document.scripts || $('script'), jsPath = js[0].src;
        jsPath1 = jsPath.substring(0, jsPath.lastIndexOf("/"));
        var cssPath = jsPath1.substring(0, jsPath1.lastIndexOf("/") + 1);
        for (var i = 0; i < name.length; i++) {
            var node = document.createElement('link');
            node.setAttribute('type', 'text/css');
            node.setAttribute('rel', 'stylesheet');
            node.setAttribute('href', cssPath + name[i]);
            head.appendChild(node);
        }
    },
    $: function (fuc) {
        sdF.fuc.push(fuc);
        sdF.fuc[sdF.fuc.length - 1].start = false;
    },
    fuc: [],
    $_start: function () {
        for (var i = 0; i < sdF.fuc.length; i++) {
            if (!sdF.fuc[i].start) {
                try { sdF.fuc[i](); sdF.fuc[i].start = true; } catch (err) { sdF.fuc[i].start = false; }
            }
        }
    }
}

sdF.useJs(["jquery.form.js", "layer.js", "bootstrap.min.js", "totalPage.js", "bootstrap-datepicker.js", "bootstrap-datetimepicker.js", "jquery.ketchup.all.js", "uploadfile.js", "kindeditor.js", "prettify.js"]);
sdF.getJs(["bootstrap-datetimepicker.zh-CN.js","zh_CN.js"],function(){
    sdF.$_start();btd.func.bodyshow();
});
sdF.useCss(["css/layer.css","css/datepicker.css","css/bootstrap-datetimepicker.min.css","css/jquery.ketchup.css","css/jquery.fileupload.css","css/jquery.fileupload-ui.css","css/bootstrap.min.css","css/font-awesome.min.css","css/btwz.css"]);

btd.func.bodyhide(); 
window.onload = function () {
    bt.start();
}

document.onreadystatechange = btd.loadSomething.onreadystatechange;


